package com.example.homework7;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.google.gson.Gson;

public class MainActivity extends Activity {
	private CookieStore cookieStore;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button button = (Button) this.findViewById(R.id.login_button);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				loginAndGetContent();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void loginAndGetContent() {
		AsyncTask<Void, Integer, Void> task = new AsyncTask<Void, Integer, Void>() {
			@Override
			protected Void doInBackground(Void... params) {
				try {
					EditText idEditText = (EditText) findViewById(R.id.main_id);
					EditText pwEditText = (EditText) findViewById(R.id.main_pw);

					String url = "https://logins.daum.net/accounts/mobile.do";
					List<NameValuePair> p = new ArrayList<NameValuePair>();
					p.add(new BasicNameValuePair("id", idEditText.getText().toString()));
					p.add(new BasicNameValuePair("pw", pwEditText.getText().toString()));
					p.add(new BasicNameValuePair("url", "http://www.daum.net"));

					DefaultHttpClient client = new DefaultHttpClient();
					HttpPost request = new HttpPost(url);

					HttpEntity entity = new UrlEncodedFormEntity(p, "UTF-8");
					request.setEntity(entity);

					HttpResponse response = client.execute(request);
					cookieStore = client.getCookieStore();

					Header header = response.getFirstHeader("X-DaumLogin-Error");
					if (header != null) {
						if ("200 OK".equals(header.getValue())) {
							getContent();
						}
					}	
				} catch (Exception e) {
					Log.e("TEST", "error", e);
				}
				
				return null;
			}
		};
		task.execute();
	}

	public void getContent() {
		AsyncTask<Void, Integer, HttpResponse> task = new AsyncTask<Void, Integer, HttpResponse>() {
			@Override
			protected HttpResponse doInBackground(Void... params) {
				try {
					String url = "http://cloud.daum.net/disk/root.json";
					DefaultHttpClient client = new DefaultHttpClient();
					client.setCookieStore(cookieStore);
					HttpGet request = new HttpGet(url);
					return client.execute(request);
				} catch (Exception e) {
					Log.e("TEST", "error", e);
				}

				return null;
			}

			@Override
			protected void onPostExecute(HttpResponse response) {
				if (response == null) {
					return;
				}
				
				try {
					String json = IOUtils.toString(response.getEntity().getContent());
					CloudResponse cloudResponse = new Gson().fromJson(json, CloudResponse.class);
					List<Content> contentList = cloudResponse.getData().getMetaInfo().getContents();

					ListView listView = (ListView) findViewById(R.id.listView1);
					SampleAdapter sampleAdapter = new SampleAdapter(MainActivity.this, contentList);
					listView.setAdapter(sampleAdapter);
				} catch (Exception e) {
					Log.e("TEST", "error", e);
				}
			}
		};
		task.execute();
	}
}
